home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / MCASM.RAR / MC_ASM.EXE / WROX_ASM / CH12 / FORMATS / FORMCONV.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-26  |  6.8 KB  |  292 lines

  1. /* This program contains
  2. *  the bodies of routines that convert image forms from one to another
  3. *  All of them work with a sigle line of image.
  4. *
  5. *  And one important note!!!
  6. *  YOU MUST reserve one extra byte in dest. line, because 
  7. * (please, forgive me) some procedures tends to write to this byte
  8. *
  9. * Written by E.Podvoysky & Kiselev J. CZ 1994.
  10. */
  11.  
  12. /*  see image form descriptions in grfile.h */
  13.  
  14. #pragma -ml
  15.  
  16. #include <mem.h>
  17. #include "common.h"
  18. #include "formconv.h"
  19.  
  20. void invert_order(BYTE *source,BYTE *dest,int width) {
  21.     register int i;
  22.     register BYTE *s,*d;
  23.  
  24.     s = source; d = dest + width - 1;
  25.     for (i = width; i > 0;  i--) *(d--) = *(s++);
  26.  
  27. }
  28.  
  29. void mono2row(BYTE *source,BYTE *dest,BYTE whitecolor,int width){
  30. register int i=0,j;
  31. register BYTE b;
  32.     while (TRUE) {
  33.         b = *(source++);
  34.         for (j = 0; j < 8; j++) {
  35.             *(dest++) = (b & 0x80) ? whitecolor : 0;
  36.             b <<= 1;
  37.             if (i++ >= width) return;
  38.             }
  39.      }
  40. }
  41.  
  42. void monoGIF2mono(BYTE *source,BYTE *dest,int width) {
  43. register int j;
  44.     for(j=0;j<width;j++)
  45.         if(source[j] == 0) dest[j/8] <<= 1;
  46.         else {dest[j/8] <<= 1; dest[j/8]++;}
  47. }
  48.  
  49. void mono2GIFmono(BYTE *source,BYTE *dest,int width) {
  50. mono2row(source,dest,1,width);
  51. }
  52.  
  53. void mono2mono(BYTE *source,BYTE *dest,int width) {
  54. memmove(dest,source,(width+7)/8);
  55. }
  56.  
  57. void gray2byte(BYTE *source,BYTE *dest,int width) { // = byte2gray
  58. memmove(dest,source,width);
  59. }
  60.  
  61. void col2562byte(BYTE *source,BYTE *dest,int width) { // = byte2col256
  62. memmove(dest,source,width);
  63. }
  64.  
  65. void col16A2col16B(BYTE *source,BYTE *dest,int width) { //16col2byte
  66. register int i;
  67. register BYTE b;
  68.     for (i = (width+1)/2; i > 0;  i--) {
  69.            b = *(source++);
  70.            *(dest++) = b >> 4;
  71.            *(dest++) = b & 0x0F;
  72.            }
  73. }
  74.  
  75. void col16plane2col16B(BYTE *source,BYTE *dest,int width){ //16col2byte
  76. register int j;
  77. for(j=0;j<width;j++)
  78. // 4 planes
  79.     *(dest++) = ((source[j/8] & (0x80 >> j%8) ) >> 7-j%8) |
  80.     (((source[j/8+(width+7)/8] & (0x80 >> j%8)) >> 7-j%8) << 1) |
  81.     (((source[j/8+(width+7)/8*2] & (0x80 >> j%8)) >> 7-j%8) << 2) |
  82.     (((source[j/8+(width+7)/8*3] & (0x80 >> j%8)) >> 7-j%8) << 3);
  83. }
  84.  
  85. void col16B2col16A(BYTE *source,BYTE *dest,int width) {
  86. register int i;
  87. for(i=(width+1)/2;i>0;i--) {
  88.     *(dest) = *(source++) << 4;
  89.     *(dest++) |= *(source++);
  90. }
  91. }
  92.  
  93. void col16B2col16plane(BYTE *source,BYTE *dest,int width){
  94.  
  95. register BYTE b;
  96. register int i,j;
  97. BYTE *p0 = dest;
  98. BYTE *p1 = &dest[(width+7)/8];
  99. BYTE *p2 = &dest[(width+7)/8*2];
  100. BYTE *p3 = &dest[(width+7)/8*3];
  101.  
  102. for(i = (width+7)/8; i > 0;  i--) {
  103.     for(j = 0; j < 8; j++) {
  104.         b =  *(source++);
  105.         *p0 = (*p0 << 1) | (b & 1);
  106.         *p1 = (*p1 << 1) | ((b >> 1) & 1);
  107.         *p2 = (*p2 << 1) | ((b >> 2) & 1);
  108.         *p3 = (*p3 << 1) | (b >> 3);
  109.     }
  110.     p0++; p1++; p2++; p3++;
  111. }
  112. }
  113.  
  114. void mono2RGBlines(BYTE *source, BYTE *dest,int width) {
  115. register int j;
  116. BYTE *rp = dest;
  117. BYTE *gp = &dest[width];
  118. BYTE *bp = &dest[width*2];
  119. for(j=0;j<width;j++) {
  120.     if((source[j/8] << 7-j%8) == 1) *(rp++) = *(gp++) = *(bp++) = 255;
  121.     else *(rp++) = *(gp++) = *(bp++) = 0;
  122. }
  123. }
  124.  
  125. void byte2RGBlines(BYTE *source, BYTE *dest,int width,BGRpalette pal) {
  126. register int j;
  127. BYTE *rp = dest;
  128. BYTE *gp = &dest[width];
  129. BYTE *bp = &dest[width*2];
  130. for(j=0;j<width;j++) {
  131.     *(rp++) = pal[source[j]].r;
  132.     *(gp++) = pal[source[j]].g;
  133.     *(bp++) = pal[source[j]].b;
  134. }
  135. }
  136.  
  137. void BGR2RGBlines(BYTE *source, BYTE *dest, int width) {
  138. register int i;
  139. register BYTE *src;
  140. BYTE *rp = dest;
  141. BYTE *gp = &dest[width];
  142. BYTE *bp = &dest[width*2];
  143. src = source;
  144. for (i = width; i > 0; i--) {
  145.     *(bp++) = *(src++);
  146.     *(gp++) = *(src++);
  147.     *(rp++) = *(src++);
  148. }
  149. }
  150.  
  151. void RGB15_2RGBlines(BYTE *source, BYTE *dest, int width) {
  152. int i;
  153. register WORD w;
  154. register WORD *src;
  155. BYTE *rp = dest;
  156. BYTE *gp = &dest[width];
  157. BYTE *bp = &dest[width*2];
  158. src = (WORD *)source;
  159. for (i = width; i > 0; i--) {
  160.     w = *(src++);
  161.     *(rp++) = ((w >> 10) & 0x1F) << 3;
  162.     *(gp++) = ((w >> 5) & 0x1F) << 3;
  163.     *(bp++) = ( w & 0x1F) << 3;
  164. }
  165. }
  166.  
  167. void RGB16_2RGBlines(BYTE *source, BYTE *dest, int width) {
  168. int i;
  169. register WORD w;
  170. register WORD *src;
  171. BYTE *rp = dest;
  172. BYTE *gp = &dest[width];
  173. BYTE *bp = &dest[width*2];
  174.  
  175. src = (WORD *)source;
  176. for (i = width; i > 0; i--) {
  177.     w = *(src++);
  178.     *(rp++) = ((w >> 11) & 0x1F) << 3;
  179.     *(gp++) = ((w >> 6) & 0x3F) << 2;
  180.     *(bp++) =  (w & 0x1F) << 3;
  181. }
  182. }
  183.  
  184. void BGRA32_2RGBlines(BYTE *source, BYTE *dest, int width) {
  185. register int i;
  186. register BYTE *src;
  187. BYTE *rp = dest;
  188. BYTE *gp = &dest[width];
  189. BYTE *bp = &dest[width*2];
  190.  
  191. src = source;
  192. for (i = width; i > 0; i--) {
  193.     *(bp++) = *(src++);
  194.     *(gp++) = *(src++);
  195.     *(rp++) = *(src++);
  196.     src++;
  197. }
  198. }
  199.  
  200. void RGBplanes2RGBlines(BYTE *source, BYTE *dest, int width) {
  201.      memcpy(dest,source,width*3);
  202. }
  203.  
  204. void RGBlines2BGR(BYTE *source, BYTE *dest, int width) {
  205. register int i;
  206. BYTE *rp = source;
  207. BYTE *gp = &source[width];
  208. BYTE *bp = &source[width*2];
  209.  
  210. for(i=0;i<width;i++) {
  211.     *(dest++) = *(bp++);
  212.     *(dest++) = *(gp++);
  213.     *(dest++) = *(rp++);
  214. }
  215. }
  216.  
  217. void RGBlines2RGB15(BYTE *source, BYTE *dest, int width) {
  218. register int i;
  219. register WORD *dst = (WORD *)dest;
  220. BYTE *rp = source;
  221. BYTE *gp = &source[width];
  222. BYTE *bp = &source[width*2];
  223.  
  224. for (i = width; i > 0; i--)
  225.     *(dst++) = (*(rp++) << 10) | (*(gp++) << 5) | *(bp++);
  226. }
  227.  
  228. void RGBlines2BGRA32(BYTE *source, BYTE *dest, int width) {
  229. register int i;
  230. BYTE *rp = source;
  231. BYTE *gp = &source[width];
  232. BYTE *bp = &source[width*2];
  233.  
  234. for(i=0;i<width;i++) {
  235.     *(dest++) = *(bp++);
  236.     *(dest++) = *(gp++);
  237.     *(dest++) = *(rp++);
  238.     *(dest++) = 0;
  239. }
  240. }
  241.  
  242. void RGBlines2gray(BYTE *source, BYTE *dest, int width) {
  243. int j;
  244. BYTE *rp = source;
  245. BYTE *gp = &source[width];
  246. BYTE *bp = &source[width*2];
  247. for (j = 0; j < width; j++)
  248.     *(dest++) = (*(rp++) * 299L + *(gp++) * 587L + *(bp++) * 114L) / 1000;
  249. }
  250.  
  251. void palette2gray(BGRpalette source,BGRpalette dest) {
  252. int j;
  253. for (j = 0; j < 256; j++)
  254.     dest[j].g = dest[j].b = dest[j].r =
  255.       (source[j].r * 299L+ source[j].g * 587L + source[j].b * 114L + 500) / 1000;
  256. }
  257.  
  258. void paletted2gray(BYTE *source, BYTE *dest,int width, BGRpalette graypal) {
  259. int j;
  260. for (j = width; j > 0; j--)
  261.     *(dest++) = graypal[*(source++)].r;
  262. }
  263.  
  264. void mono2RGB24(BYTE *source,BYTE *dest,int width) {
  265. int j,i = 0;
  266. BYTE b;
  267. BYTE *rp = dest;
  268. BYTE *gp = &dest[width];
  269. BYTE *bp = &dest[width*2];
  270.  
  271. while (TRUE) {
  272.         b = *(source++);
  273.         for (j = 0; j < 8; j++) {
  274.             *(rp++) = *(gp++) = *(bp++) = (b & 0x80) ? 255 : 0;
  275.             b <<= 1;
  276.             if (i++ >= width) return;
  277.             }
  278.      }
  279. }
  280.  
  281. getRGBlines getRGBproc(image_type source_type, image_extention_type source_ext) {
  282.     if (source_type != TRUECOLORIMG) return(NULL);
  283.  
  284.     switch(source_ext) {
  285.         case RGB15: return(RGB15_2RGBlines);
  286.         case RGB16: return(RGB16_2RGBlines);
  287.         case BGR24: return(BGR2RGBlines);
  288.         case BGRA32:return(BGRA32_2RGBlines);
  289.         case RGBPLANE24: return(RGBplanes2RGBlines);
  290.         }
  291. }
  292.